package com.huonan.utils;

import cn.hutool.extra.ftp.FtpConfig;
import cn.hutool.extra.ssh.JschSessionPool;
import cn.hutool.extra.ssh.JschUtil;
import cn.hutool.extra.ssh.Sftp;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;

import java.nio.charset.Charset;
import java.util.List;
import java.util.stream.Collectors;

public class SftpUtil {

    private static Sftp getInstance() {
        FtpConfig ftpConfig = new FtpConfig();
        ftpConfig.setHost("dts-nb.mininglamp.com")
                .setPort(2222)
                .setUser("social_cd_test")
                .setPassword("A2i7p+62R")
                .setCharset(Charset.defaultCharset());
        return new Sftp(ftpConfig);
    }

    public static List<String> lsFiles(String srcPath){
        Sftp sftp1 = JschUtil.createSftp("", 1, "", "");
        sftp1.close();
        JschSessionPool instance = JschSessionPool.INSTANCE;
        Session session = instance.getSession("", 1, "", "");
        try {
            session.connect();
        } catch (JSchException e) {
            throw new RuntimeException(e);
        }
        try ( Sftp sftp = getInstance();) {
            sftp.close();
            List<String> list = sftp.lsFiles(srcPath);
            return list.stream().map(f -> srcPath + "/" + f).collect(Collectors.toList());
        }

    }

}
